import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { getUserAccount, getUserSubcount } from '@/utils/api'
import { UserProfile, UserSubcount } from '@/modules/User'
import produce, { Draft } from 'immer';
import { getStoreData } from '@/utils/checkKeyStorage';

const userAccount = <UserProfile>getStoreData('userAccount', {})
const userSubcount = <UserSubcount>getStoreData('userSubcount', {})

const userSlice = createSlice({
    name: 'userSlice',
    initialState: {
        userAccount,
        userSubcount
    },
    reducers: {

    },
    extraReducers: (builder) => {
        builder.addCase(getUserAccount.fulfilled, (state, action) => {
            state.userAccount = action.payload;
            sessionStorage.setItem('userAccount', JSON.stringify(state.userAccount))
        });
        builder.addCase(getUserSubcount.fulfilled, (state, action) => {
            state.userSubcount = action.payload
            sessionStorage.setItem('userSubcount', JSON.stringify(state.userSubcount));
        });
    }
})


export const { } = userSlice.actions

export const { reducer: userReducer } = userSlice